package com.dc.seckill.service;

import com.dc.seckill.dao.MiaoshaUserDao;
import com.dc.seckill.domain.MiaoshaUser;
import com.dc.seckill.exception.GlobalException;
import com.dc.seckill.redis.MiaoshaUserKey;
import com.dc.seckill.redis.RedisService;
import com.dc.seckill.result.CodeMsg;
import com.dc.seckill.util.MD5Utils;
import com.dc.seckill.util.UUIDUtil;
import com.dc.seckill.vo.LoginVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

/**
 * @模块 用户模块
 * @作者 djs
 * @时间 2018/3/19 11:28
 * @版本 V1.0
 * @描述 进行用户的一些业务操作
 */
@Service("miaoshaUserService")
public class MiaoshaUserService {

    public Logger log = LoggerFactory.getLogger(MiaoshaUserService.class);

    @Autowired
    private MiaoshaUserDao userDao;

    @Autowired
    private RedisService redisService;

    /**
     * 进行登录
     * @params [response, loginVo]
     * @returns com.dc.seckill.result.CodeMsg
     */
    public CodeMsg login(HttpServletResponse response, LoginVo loginVo){
        //1:参数校验
        if(loginVo == null){
            throw new GlobalException(CodeMsg.SERVER_ERROR);
        }
        //2:判断会员是否存在
        String moblie = loginVo.getMobile();    //输入的手机号
        String formPass = loginVo.getPassword();    //输入的密码
        //判断手机号是否存在
        MiaoshaUser miaoshaUser = userDao.getById(Long.parseLong(moblie));
        if(miaoshaUser == null){
            throw new GlobalException(CodeMsg.MOBILE_NOT_EXIST);
        }
        //3:校验密码
        String dbPass= miaoshaUser.getPassword();
        String saltDb = miaoshaUser.getSalt();
        String calcPass = MD5Utils.formPassToDBPass(formPass,saltDb);
        if(!calcPass.equals(dbPass)){
            throw new GlobalException(CodeMsg.PASSWORD_ERROR);
        }

        //4:将用户信息存入session
        String token = UUIDUtil.uuid();
        addcokie(response,token,miaoshaUser);

        return CodeMsg.SUCCESS;
    }

    /**
     * 根据token获取用户信息
     * @params [response, token]
     * @returns com.dc.seckill.domain.MiaoshaUser
     */
    public MiaoshaUser getByToken(HttpServletResponse response, String token){
        if(StringUtils.isEmpty(token)){
            return null;
        }
        MiaoshaUser miaoshaUser = redisService.get(MiaoshaUserKey.token,token,MiaoshaUser.class);

        if(miaoshaUser != null){
            //更新有效期
            addcokie(response,token,miaoshaUser);
        }
        return miaoshaUser;

    }

    /**
     * 将token存入本地缓存中
     * @params [response, token, miaoshaUser]
     * @returns void
     */
    public void addcokie(HttpServletResponse response, String token ,MiaoshaUser miaoshaUser) {
        redisService.set(MiaoshaUserKey.token,token,miaoshaUser);
        //将信息存入cookie
        Cookie cookie = new Cookie(MiaoshaUserKey.COOKI_NAME_TOKEN,token);
        cookie.setPath("/");//存放至根目录
        cookie.setMaxAge(MiaoshaUserKey.token.expireSeconds());
        response.addCookie(cookie);

    }


    /**
     * 通过ID获取用户
     * @params [id]
     * @returns com.dc.seckill.domain.MiaoshaUser
     */
    public MiaoshaUser getById(Long id){
        return userDao.getById(id);
    }

}
